package com.admin.kotlintest.activity.car

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.CheckBox
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.admin.kotlintest.R
import com.admin.kotlintest.adapter.ChoseCarAppiontmentAdapter
import com.admin.kotlintest.adapter.ChoseCarListAdapter
import com.admin.kotlintest.adapter.ChoseCarListAdapter.CarItemCilckListener
import com.admin.kotlintest.base.BaseActivity
import com.admin.kotlintest.bean.Appointment
import com.admin.kotlintest.bean.Car
import com.socks.library.KLog
import kotlinx.android.synthetic.main.activity_chose_car.*
import kotlinx.android.synthetic.main.layout_title.*
import org.jetbrains.anko.toast
import java.util.*
import com.admin.kotlintest.bean.Person



/**
 * Created by 吴昶 on 2018/4/19.
 */
class ChoseCarActivity:BaseActivity(),CarItemCilckListener{

    var carAdapter:ChoseCarListAdapter?=null
    var checkBox:CheckBox?=null
    var clickPosition=-1
    var car:Car?=null
    var appiontmentAdapter:ChoseCarAppiontmentAdapter?= null
    var appiontList:MutableList<Appointment>?=null

    override fun ItemClickListener(item: Car, box: CheckBox?,position: Int) {
        if(clickPosition!=position) {
            checkBox?.let { checkBox!!.isChecked = false }
            checkBox = box
            car=item
            showCarHasAppiontmentList(car)
        }else{
            car = if(box!!.isChecked){
                showCarHasAppiontmentList(item)
                item
            }else{
                null
            }
        }
        clickPosition=position
    }
    var handler = Handler(Handler.Callback {
        when (it.what){
            2->{
                if(appiontList!!.size==0){
                    noAppiontment()
                }else{
                    appiontmentAdapter!!.upData(appiontList!!)
                    tv_no_appiontment.visibility=View.GONE
                    linear_appiontment.visibility=View.VISIBLE
                }
            }
        }
        false
    })
    override fun bindLayout(): Int {
        return R.layout.activity_chose_car
    }

    override fun initData() {
        tv_title.text="选择车辆"
        linear_title_right.visibility= View.VISIBLE
        tv_title_right.visibility=View.VISIBLE
        tv_title_right.text="确认"
        appiontList=ArrayList<Appointment>()
        appiontmentAdapter= ChoseCarAppiontmentAdapter(context,R.layout.layout_chose_car_appiontment_item, null)
        lv_appiontment.adapter=appiontmentAdapter
        var query=BmobQuery<Car>()
        query.include("driver")
        query.findObjects(object :FindListener<Car>(){
            override fun done(p0: MutableList<Car>?, p1: BmobException?) {
                p1?:let {
                    p0?:let {
                        toast("无可用车辆")
                        tv_no_disabled_car.visibility=View.VISIBLE
                    }
                    p0?.let {
                        if(p0.size==0){
                            tv_no_disabled_car.visibility=View.VISIBLE
                        }else {
                            carAdapter = ChoseCarListAdapter(context, R.layout.layout_car_list_item, p0, lv_car_list, this@ChoseCarActivity)
                        }
                    }
                }
                p1?.let {
                    KLog.d(p1.message)
                    tv_no_disabled_car.visibility=View.VISIBLE
                }
            }
        })
    }

    override fun initEvent() {
        tv_title_right.setOnClickListener {
            car?.let {
                var intent=Intent()
                var bundle=Bundle()
                bundle.putSerializable("car",car)
                intent.putExtras(bundle)
                setResult(Activity.RESULT_OK,intent)
                finish()
            }
            car?:let {
                toast("请选择车辆")
            }
        }

    }

    fun showCarHasAppiontmentList(car: Car?){
        car?.let {
            appiontList!!.clear()
            for(m in 0..2){
                var query=BmobQuery<Appointment>()
                query.addWhereEqualTo("appointState",m)
                query.include("car")
                query.findObjects(object:FindListener<Appointment>(){
                    override fun done(p0: MutableList<Appointment>?, p1: BmobException?) {
                        p1?:let {
                            p0?.let {
                                appiontList!!.addAll(p0)
                                handler.sendEmptyMessage(2)
                            }
                        }
                        p1?.let {

                        }
                    }
                })
            }
        }
        car?:let {
            noAppiontment()
        }
    }

    fun noAppiontment(){
        tv_no_appiontment.visibility=View.VISIBLE
        linear_appiontment.visibility=View.GONE
    }
}